package com.frank.activedatabus.broadcast;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.utils.TextTool;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.commonevent.CommonEventPublishInfo;
import ohos.rpc.RemoteException;

/**
 * @version 1.0.1
 * @description:发布事件
 * @program: activedatatest
 * @Author xiaozhijun
 * @Date 2020/12/25 15:08
 */
public class BroadcastPublishHelper {

    //事件
    private CommonEventData commonEventData;

    //发布的参数
    private CommonEventPublishInfo commonEventPublishInfo;


    private BroadcastPublishHelper(CommonEventData commonEventData, CommonEventPublishInfo publishInfo) {
        this.commonEventData = commonEventData;
        this.commonEventPublishInfo = publishInfo;
    }

    public static class EventPublishBuild {

        private Intent buildIntent;

        private String data;

        private int code = 0;

        private boolean isSetCode = false;

        CommonEventPublishInfo publishInfo;

        public EventPublishBuild(Operation operation) {
            buildIntent = new Intent();
            buildIntent.setOperation(operation);
        }

        /**
         * 增加IntentParams参数
         *
         * @param params
         * @return
         */
        public EventPublishBuild withIntentParams(IntentParams params) {
            buildIntent.setParams(params);
            return this;
        }

        /**
         * 增加CommonEventData Data数据
         *
         * @param sData
         * @return
         */
        //todo 当前版本发布以后取不到,找到解决方案再完善
        private EventPublishBuild withData(String sData) {
            this.data = sData;
            return this;
        }

        /**
         * 发布有序事件
         *
         * @return
         */
        public EventPublishBuild withOrdered() {
            if (publishInfo == null) {
                publishInfo = new CommonEventPublishInfo();
            }
            publishInfo.setOrdered(true);
            return this;
        }

        /**
         * 发布带权限事件
         *
         * @param permissions
         * @return
         */
        public EventPublishBuild withPermissions(String[] permissions) {
            if (publishInfo == null) {
                publishInfo = new CommonEventPublishInfo();
            }
            publishInfo.setSubscriberPermissions(permissions);
            return this;
        }

        /**
         * 发布粘性事件
         * @description 如果有一次设置成了粘性事件，后续改成非粘性并不可靠
         * @return
         */
        public EventPublishBuild withSticky(boolean isSticky) {
            if (publishInfo == null) {
                publishInfo = new CommonEventPublishInfo();
            }
            publishInfo.setSticky(isSticky);
            return this;
        }

        /**
         * 增加 CommonEventData Code码
         *
         * @param code
         * @return
         */
        //todo 当前版本发布以后取不到,找到解决方案再完善
        private EventPublishBuild withCode(int code) {
            this.code = code;
            isSetCode = true;
            return this;
        }

        public BroadcastPublishHelper build() {
            CommonEventData eventData = new CommonEventData(buildIntent);
            if (data != null && !"".equals(data)) {
                eventData.setData(data);
            }
            if (isSetCode) {
                eventData.setCode(code);
            }
            if (publishInfo == null) {
                withSticky(false);
            }
            return new BroadcastPublishHelper(eventData, publishInfo);
        }
    }

    /**
     * 发布公共事件
     */
    public void publishEvent() {
        try {
            if (commonEventData != null && commonEventData.getIntent() != null &&
                    !TextTool.isNullOrEmpty(commonEventData.getIntent().getAction())) {
                if (commonEventPublishInfo != null) {
                    CommonEventManager.publishCommonEvent(commonEventData, commonEventPublishInfo);
                } else {
                    CommonEventManager.publishCommonEvent(commonEventData);
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

}
